<template>
	<view class="formClass">
		<view><u-toast ref="uToast" /></view>
		<u-form :model="form" ref="uForm" >
			<u-form-item label="wifi(路由器):" label-width="170rpx" v-for="(item,index) in form.content" :key="index" prop="content">
				<view style="display: flex; justify-content: space-between; width: 90%;">
					<u-input v-model="item.name" border placeholder="请输入wifi名称" style="width: 70%;"></u-input>
					<u-image width="60rpx" height="60rpx" src="https://jiaoguanyi008.oss-cn-shenzhen.aliyuncs.com/miniPro/images/imgs/del.png" style="padding: 10rpx;" @click="removeDomain(domain)"></u-image>
				</view>
			</u-form-item>
			<u-form-item>
				<view @click="addDomain()" style="color: royalblue;">继续添加wifi(路由器)</view>
			</u-form-item>
		</u-form>
		<view style="display: flex; justify-content: space-between;">
			<u-button @click="submit()" type="primary" shape="circle" :custom-style="customStyle">提交</u-button>
			<u-button @click="cancel" shape="circle" :custom-style="customStyle">取消</u-button>
		</view>
	</view>
</template>

<script>
import { addWifiList } from '../../../api/systemControl'
	
	export default {
		data() {
			return {
				show: false,
				form: {
					content: [{name: ''}],
				},
				customStyle: {
					magin: '0rpx',
					padding: '0rpx',
					width: '200rpx',
					height: '80rpx'
				},
				rules: {
					content: [{required: true, message: '请添加wifi名称', trigger: 'blur'}],
				}
			}
		},
		onLoad() {
		},
		methods: {
			addDomain() {
				if (this.form.content.length > 20) {
					this.$refs.uToast.show({
						title: '限制添加20条',
						type: 'info',
						icon: false
					})
				}
				this.form.content.push({name: ''})
			},
			removeDomain(item) {
				var index = this.form.content.indexOf(item)
				if (index !== -1) {
					this.form.content.splice(index, 1)
				}
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						let addForm = JSON.parse(JSON.stringify(this.form))
						addForm.content = addForm.content.map(item => item.name).toString()
						addForm.userId = uni.getStorageSync('userId')
						addForm.id = uni.getStorageSync('snId')
						addForm.last_ip = uni.getStorageSync('Ip')
						addWifiList(addForm).then(res => {
							this.$refs.uToast.show({
								title: res.msg,
								type: 'success',
								icon: false
							})
							setTimeout(() => {
								let pages = getCurrentPages(); //获取所有页面栈实例列表
								let prevPage = pages[pages.length - 2]; //上一页页面实例
								uni.navigateBack({ //uni.navigateTo跳转的返回，默认1为返回上一级
											delta: 1,
											success:(event)=>{
												prevPage.$vm.queryWifiList();  // getData 是自己对应的方法名
											}
									});
							}, 500)
							
						}).catch(err => {
							this.$refs.uToast.show({
								title: err.msg,
								type: 'error',
								icon: false
							})
						})
					}else {
						console.log('验证失败')
					}
				})
			},
			cancel() {
				console.log('取消提交')
			}
			
		}
	}
</script>

<style lang="scss">
	.formClass {
		background-color: #ffffff;
		border-style: solid;
		border-color: #ffffff;
		border-radius: 20rpx;
		margin: 15rpx;
		padding: 10rpx;
	}
</style>